import requests
import hashlib
import base64
import configparser
import json

# md5加密
def md5_crypt(_str):
    md5_pass = hashlib.md5()
    md5_pass.update(_str.encode("utf8"))
    return md5_pass.hexdigest()
# print(md5_crypt("junetiger"))

# base64 编码
def b64_crypt(_str):
    # b64 = base64.b64decode('c2FsdF8xMTEyMzQ1Ng==')
    # print("解码",b64.decode('utf8'))
    crypt_pass = "salt_1l" + _str
    b64_pass = base64.b64encode(crypt_pass.encode("utf8")).decode("utf8")
    return b64_pass
# print(b64_crypt("junetiger"))


class Ikuai_interface:
    def __init__(self, _url="", _user="", _pass=""):
        self.url = "http://" + _url
        self.user = _user
        self.passwd = md5_crypt(_pass)
        self.b64 = b64_crypt(_pass)
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE",
            "Content-Type": "application/json;charset=UTF-8",
            "Accept": "application/json, text/plain, */*",
            "Accept-Encoding": "gzip, deflate"
            # "Cookie": "sess_key=2ad997aa76c9e23c8d29dc8719c0743e; username=admin; login=1"
        }

    def login(self, _active="/Action/login"):
        login_data = {
            "username": self.user,
            "passwd": self.passwd,
            "pass": self.b64,
            "remember_password": "",
        }
        print('登陆数据')
        print(login_data)
        req = requests.post(
            url=self.url + _active, headers=self.headers, json=login_data
        )
        res = eval(req.content.decode("utf8"))
        print(res)
        if int(res["Result"]) == 10000:
            print("登录成功")
        else:
            print("登录失败")

        cookies = req.cookies.items()
        return cookies

    def get_totalinfo(self, _cookie="", _active="/Action/call"):
        data = {'func_name': "homepage", 'action': "show", 'param': {'TYPE': "sysstat,ac_status"}}
        self.cookies = {"sess_key": _cookie, "username": "admin", "login": "1"}
        print(self.url + _active)
        print(self.cookies)
        req = requests.post(
            url=self.url + _active,
            headers=self.headers,
            json=data,
            cookies=self.cookies,
        )
        res = eval(req.content.decode("utf8"))
        print("*" * 80)
        result =json.dumps(res,indent=4)
        print(result)



cfg = configparser.ConfigParser()
cfg.read("conf/login.ini")
# --------------------------------------------------

url = cfg.get("logininfo", "host")
user = cfg.get("logininfo", "user")
passwd = cfg.get("logininfo", "pass")
cookie = cfg.get("logininfo", "cookie")
ikuai = Ikuai_interface(_url=url, _user=user, _pass=passwd)


if cookie:
    print("已存在cookies ", cookie)
    ikuai.get_totalinfo(cookie)
else:
    # cookies 不存在 账号密码登录
    ck = ikuai.login()
    print("登录成功，获取到cookies", ck)
    ck = ck[0][1]
    cfg.set("logininfo", "cookie", ck)
    with open("conf/login.ini", "w") as conf:
        cfg.write(conf)
    ikuai.get_totalinfo(ck)


